﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using VkVideoPlayer.UI.ViewModels;
namespace VkVideoPlayer.UI
{
	public partial class AuthPage : PhoneApplicationPage
	{
		public AuthPage()
		{
			InitializeComponent();
			Loaded += AuthPage_Loaded;
		}

		void AuthPage_Loaded(object sender, RoutedEventArgs e)
		{
			this.DataContext = ViewModelLocator.Instance.AuthViewModel;
			if(ViewModel.IsAuthorized)
			{
				ViewModel.RestoreContext();
				NavigationService.GoBack();
			}
			else
			{
				//ApplicationBar.IsVisible = true;
				var uri = ViewModel.GetUri();
				webBrowser1.Navigated += webBrowser1_Navigated;	
					webBrowser1.NavigationFailed += Failed;
				webBrowser1.Navigate(uri);
			}
		}
		void Failed(object sender, NavigationFailedEventArgs e)
		{
			webBrowser1.Visibility = Visibility.Collapsed;
			NavigationFailed.Visibility = Visibility.Visible;
		}
		void webBrowser1_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
		{
			ViewModel.ParseUri(e.Uri);
		}

		public AuthViewModel ViewModel
		{
			get { return this.DataContext as AuthViewModel; }
		}

		protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
		{
			base.OnNavigatedFrom(e);
			//ViewModel.OnNavigatedFrom(this, e);
		}

		protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
		{
			base.OnNavigatedTo(e);
		//	ViewModel.OnNavigatedTo(this, e);
		}
	}
}